package com.smarthome;

import com.smarthome.tools.CoverFlow;
import com.smarthome.tools.HttpUrlConnection7;
import com.smarthome.tools.ImageHandler;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class CoverFlowActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CoverFlow cFlow=new CoverFlow(this);
        ImageAdapter imageAdapter = new ImageAdapter(this);

        cFlow.setAdapter(imageAdapter);
        
        cFlow.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView parent, View v, int position, long id) {
        		HttpUrlConnection7.Instance().connectToServerWithUser(13 + position);
            }
		});
        cFlow.setSelection(imageAdapter.getCount()/2);
        cFlow.setSpacing(-75);
        setContentView(cFlow);
    }
    
    public class ImageAdapter extends BaseAdapter{
    	private int imageXStart; 
    	//the start x-coordinate of the primary image located at the center
    	private int imageYStart;
    	//the start y-coordinate of the primary image located at the center
    	public final int IMAGE_WIDTH=300;
    	//the width of the primary image located at the center
    	public final int IMAGE_HEIGHT=400;
    	//the height of the primary image located at the center
    	public final int CHILD_VIEW_WIDTH = 300;
    	//the width of every child view
    	public final int CHILD_VIEW_HEIGHT = 800;
    	//the height of every child view
    	private final int REFLECTION_GAP = 20;
    	
    	int mGalleryItemBackground;  
        private Context mContext;
        private Integer[] mImageIds = {   
                R.drawable.a,   
                R.drawable.b,
                R.drawable.c,
                R.drawable.d,  
                R.drawable.e,  
                R.drawable.f,
                R.drawable.g,
                R.drawable.h,
                R.drawable.i,
                R.drawable.j,
                R.drawable.k,
                R.drawable.gallery_photo_1,
                R.drawable.gallery_photo_2,
                R.drawable.gallery_photo_3,
                R.drawable.gallery_photo_4,
                R.drawable.gallery_photo_5,
                R.drawable.gallery_photo_6,
                R.drawable.gallery_photo_7,
                };  
        public ImageAdapter(Context c) {  
            mContext = c;
        }  
    	@Override
        public int getCount() {  
            return mImageIds==null?0:mImageIds.length;  
        }  
        public Object getItem(int position) {  
            return mImageIds[position];     
        }  
        public long getItemId(int position) {  
            return position;  
        } 
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
        	ImageView iView = new ImageView(mContext);
        	Bitmap originalBitmap = 
        		((BitmapDrawable)getResources().getDrawable(mImageIds[position])).getBitmap();
        	ImageHandler iHandler = new ImageHandler(originalBitmap);
        	Bitmap scaledBitmap = iHandler.getScaledBitmap(IMAGE_WIDTH, IMAGE_HEIGHT);
        	iHandler.setBitmap(scaledBitmap);
        	int width = iHandler.getWidth();
        	int height = iHandler.getHeight();
        	
        	Bitmap bitmapWithLinearGradient = iHandler.getBitmapWithLinearGradient(
        			height * 1 / 4, 0, 0, 20);
        	iView.setImageBitmap(bitmapWithLinearGradient);
        	
        	return iView;
        }
        public int getImageXStart(){
        	return imageXStart;
        }
        public int getImageYStart(){
        	return imageYStart;
        }
    }
}